layui.use(['table', 'form', 'jquery', 'laydate', 'layer'], function () {
    let table = layui.table;
    let form = layui.form;
    let laydate = layui.laydate;
    let layer = layui.layer;
    let $ = layui.jquery;

    let BIZ_NAME = 'recharge-times';
    let BIZ_TABLE = 'recharge-times-table';
    let BIZ_FORM = 'recharge-times-form';
    let tableRechargeTimesObj;
    let formRechargeTimesObj;
    let hasRun = false;

    let cols = [
        [
            {title: '记录编号', field: 'id', align: 'center'},
            {title: '会员ID', field: 'memberId', align: 'center'},
            {title: '会员名称', field: 'memberName', align: 'center'},
            {title: '活动名称', field: 'activityName', align: 'center'},
            {title: '充次金额', field: 'amount', align: 'center'},
            {title: '充值次数', field: 'times', align: 'center'},
            {title: '剩余次数', field: 'remainingTimes', align: 'center'},
            {title: '已使用次数', field: 'usedTimes', align: 'center'},
            {title: '充值方式', field: 'rechargeType', align: 'center'},
            {title: '充值时间', field: 'rechargeTime', align: 'center'},
            {title: '操作人', field: 'operators', align: 'center'}
        ]
    ]
    window.renderRechargeTimesTable = function (where) {
        hasRun = true;
        tableRechargeTimesObj = table.render({
            elem: '#' + BIZ_TABLE,
            url: '/' + BIZ_NAME + '/page',
            where: where,
            //data: [{id:1}], // 测试数据
            text: {none: '暂无相关数据'},
            response: {
                statusCode: 200,
                msgName: 'message'
            },
            page: true,
            limit: 10,
            limits: [10, 20, 50],
            cols: cols,
            skin: 'line',
            toolbar: '#recharge-times-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });
    }

    window.rechargeTimesForm = function (obj) {
        let title = '新增';
        formRechargeTimesObj = layer.open({
            type: 1,
            title: title,
            shade: 0.1,
            area: ['70%', '85%'],
            btn: ['立即提交', '取消'],
            yes: function (index, layero) {
                $('#' + BIZ_FORM + ' [lay-submit]').click();
            },
            cancel: function (index, layero) {
                layer.close(index);
            },
            content: $('#' + BIZ_FORM),
            success: function (layero, index) {
                form.render('radio');
                if (obj) {
                    //重置表单，清空上次输入的值
                    document.getElementById("recharge-times-form").reset();
                    // 填充会员信息数据
                    $('#rechargeTimesMemberId').val(obj.id);
                    $('#rechargeTimesMemberName').val(obj.name);
                    $('#rechargeTimesOperators').val(obj.createdBy);
                } else {
                    layer.alert('请选择会员后再进行充次!', {icon: 2});
                }
            }
        });
    }

    laydate.render({
        elem: '#rechargeTimesStart'
        , type: 'datetime'
    });

    laydate.render({
        elem: '#rechargeTimesEnd'
        , type: 'datetime'
    });

    form.on('submit(' + BIZ_FORM + ')', function (data) {

        let url = '/save';
        let type = 'POST';
        //如果id不为空则调用修改接口
        if (data.field.id) {
            url = '/update'
            type = 'POST';
        }
        data.field.times = data.field.remainingTimes;

        $.ajax({
            url: '/' + BIZ_NAME + url,
            data: JSON.stringify(data.field),
            type: type,
            dataType: 'json',
            contentType: 'application/json',
            success: function (result) {
                if (result.code === 200) {
                    layer.msg(result.message, {icon: 1, time: 1000}, function () {
                        layer.close(formRechargeTimesObj);
                        //执行重载
                        table.reload('member-table', {
                            page: {
                                curr: 1 //重新从第 1 页开始
                            }
                            , where: {}
                        });
                    });
                } else {
                    layer.alert(result.message, {icon: 2});
                }
            }
        })
        return false;
    });

    if (!hasRun) {
        window.renderRechargeTimesTable('');
    }
});